package com.bpms.servlet;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;

public class ShowImage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ShowImage() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String INSTANCEID = "";
			int page = 1;
			if(request.getParameter("page")!= null  && !"".equals(request.getParameter("page").trim())) 
			try{
				page = Integer.parseInt(request.getParameter("page"));
			}catch(Exception e){
				CommonUtil.debug("ShowImage getPage Parameter exception", e);
			}
			
			if(request.getParameter("id")!= null  && !"".equals(request.getParameter("id").trim())){ 
				try{
					INSTANCEID =request.getParameter("id");
				}catch(Exception e){
					CommonUtil.debug("ShowImage getPage Parameter exception", e);
					response.sendRedirect("error.jpg");
				}
			}
			CommonUtil.debug("request id is "+request.getParameter("id"));
			String filename = showpic(INSTANCEID,page);
			byte[] bytes = read(filename);
			response.setContentType("image/jpeg;charset=GB2312");
			response.setContentLength(bytes.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();	
			
	} catch (Exception e) {
			CommonUtil.debug("ShowImage exception", e);
			response.sendRedirect("error.jpg");
		}
	}
	
	private String showpic(String id,int page){
		JdbcWorker j=null;
		try{			
			j = new JdbcWorker();
			String columnSelected = "IMAGE1";
			if(page == 2) columnSelected = "IMAGE2";
			VO keyVO = new VO();
			keyVO.setAttr("INSTANCEID", id);
			keyVO.voType = "CM_DJB";
			
			VOList vol = j.readAll(new VO(), keyVO);
			if(vol.rowCount == 1){
				VO aa = (VO)vol.listData.get(0);
				CommonUtil.debug("ShowImage.showpic file:"+ aa.getAttr(columnSelected));
				return aa.getAttr(columnSelected);
			}else
				return null;
			
		}catch(Exception e){
			CommonUtil.debug("ShowImage.showpic exception", e);
			return null;
		}
	}
	private byte[] read(String fileName) throws Exception {
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));        
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);        
        byte[] temp = new byte[1024];        
        int size = 0;        
        while ((size = in.read(temp)) != -1) {        
            out.write(temp, 0, size);        
        }        
        in.close();
        return out.toByteArray();     
    }
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
